package com.cnpinxi.task;

import com.cnpinxi.domain.ExpressInfo;
import com.cnpinxi.log.LogFactory;
import com.cnpinxi.service.ExpressInfoService;
import com.cnpinxi.service.OrderService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderStatusTask {
    protected Logger logger = LogFactory.getLogger(getClass());
    @Autowired
    OrderService orderService;
    @Autowired
    ExpressInfoService expressInfoService;

    //定时任务，每天凌晨2点执行
   @Scheduled(cron = "0 0 2 * * ?")
    public void updateOrderStatus() {
        logger.info("定时任务开始执行");
        long begin = System.currentTimeMillis();
          /*订单状态
            15天后已发货订单状态自动改为交易关闭
          */
        try{
            List<ExpressInfo> expressInfos = expressInfoService.selectExpressInfosTask();
            if(expressInfos.size()>0){
                for(ExpressInfo expressInfo:expressInfos){
                    orderService.modifyOrderStatus(expressInfo.getOrderId(), OrderService.CLOSE);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        logger.info("定时任务结束，共耗时：[" + (end-begin) / 1000 + "]秒");
    }

}
